package edu.cdnu.utils;

import java.lang.reflect.Method;
import java.util.Date;

import edu.cdnu.common.dto.CommonUserDto;
import edu.cdnu.common.dto.HomeworkDto;
import edu.cdnu.pojo.HomeworkPojo;

/**
 * 类型转换工具类
 * @author ASUS AsingZ
 */
public class TypeConversionUtils {
	/**
	 * 将subject.getPrincipal()的object类型转换为CommonUserDto类型<br/>
	 * 这里遇到问题不能强制类型转换
	 * 只能获取object的属性值来set到CommonUserDto对象中
	 * @param object
	 * @return
	 */
	public static CommonUserDto subjectPrincipalToCommonUserDto(Object object) {
		CommonUserDto commonUserDto=new CommonUserDto();
		commonUserDto.setAccount((String)getFieldValueByName("account", object));
		commonUserDto.setName((String)getFieldValueByName("name", object));
		commonUserDto.setRoleId((Integer)getFieldValueByName("roleId", object));
		commonUserDto.setId((Integer)getFieldValueByName("id", object));
		return commonUserDto;
	}
	
	/**
	 * 根据属性名获取属性值
	 * */
    private static Object getFieldValueByName(String fieldName, Object o) {
        try {  
            String firstLetter = fieldName.substring(0, 1).toUpperCase();  
            String getter = "get" + firstLetter + fieldName.substring(1);  
            Method method = o.getClass().getMethod(getter, new Class[] {});  
            Object value = method.invoke(o, new Object[] {});  
            return value;  
        } catch (Exception e) {  
            return null;  
        }  
    } 

    /**
     * HomeworkPojo 转 HomeworkDto
     * @param homeworkPojo
     * @return
     */
    public static HomeworkDto homeworkPojoToHomeworkDto(HomeworkPojo homeworkPojo) {
    	return new HomeworkDto(homeworkPojo.getId(), homeworkPojo.getTitle(), homeworkPojo.getContent(), 
    			homeworkPojo.getStartTime().getTime(), homeworkPojo.getEndTime().getTime(), homeworkPojo.getScoreWay(), 
    			homeworkPojo.getCourseId(), homeworkPojo.getClassId(), homeworkPojo.getTeacherId(), homeworkPojo.getAppendix());
    }
    /**
     * HomeworkDto 转 HomeworkPojo
     * @param homeworkDto
     * @return
     */
    public static HomeworkPojo homeworkDtoToHomeworkPojo(HomeworkDto homeworkDto) {
    	return new HomeworkPojo(homeworkDto.getId(), homeworkDto.getTitle(), homeworkDto.getContent(), 
    			new Date(homeworkDto.getStartTime()), new Date(homeworkDto.getEndTime()), homeworkDto.getScoreWay(), 
    			homeworkDto.getCourseId(), homeworkDto.getClassId(), homeworkDto.getTeacherId(), homeworkDto.getAppendix());
    }
    
}
/*

1、Date对象转换为时间戳
     
Date date = new Date();  
long times = date.getTime();  
System.out.println(times);
     
2、时间戳转换为Date日期对象

long times = System.currentTimeMillis();  
Date date = new Date(times);  
System.out.println(date);

3、时间戳转换为指定日期格式

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long times = System.currentTimeMillis(); 
String str = format.format(times);
System.out.println(str);
     
4、时间字符串<年月日时分秒毫秒 >转为 时间戳

//大写HH：24小时制，小写hh：12小时制
//毫秒：SSS
//指定转化前的格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
//转化后为Date日期格式
Date date = sdf.parse(sb.toString());
//Date转为时间戳long
long shootTime = date.getTime();
System.out.println(shootTime);

*/